package com.xnfh.field.viewmodel

import com.xnfh.common.bean.CropBean
import com.xnfh.framework.base.BaseViewModel
import com.xnfh.framework.bean.RespStateData
import org.json.JSONObject

/**
 * @Description:
 * @Author: ziheng
 * @CreateDate: 2023/11/30 13:42
 * @UpdateUser: ziheng
 * @UpdateDate: 2023/11/30 13:42
 * @UpdateRemark: 更新说明
 */
class FieldEditorViewModel : BaseViewModel() {
    private val fieldEditorViewModelRes = FieldEditorViewModelRes()
    val cropList = RespStateData<List<CropBean>>()
    val removeField = RespStateData<Any>()


    /**
     * 获取作物列表
     */
    fun requestCropList() = launch {
        val jsonObject = JSONObject()
        jsonObject.put("scLevel", "2")
        fieldEditorViewModelRes.requestCropList(creatRequestBody(jsonObject.toString()), cropList)
    }

    /**
     * 编辑地块
     */
    fun requestEditField(fieldId: Long?, cropId: String?, fieldName: String) = launch {
        val jsonObject = JSONObject()
        jsonObject.put("fieldId", fieldId)
        jsonObject.put("plantCropId", cropId)
        jsonObject.put("fieldName", fieldName)
        fieldEditorViewModelRes.requestEditField(
            creatRequestBody(jsonObject.toString()),
            removeField
        )
    }

    /**
     * 轮作地块
     */
    fun requestLzField(fieldId: Long?, cropId: String?, time: String?) = launch {
        val jsonObject = JSONObject()
        jsonObject.put("fieldId", fieldId)
        jsonObject.put("startTime", time)
        jsonObject.put("cropId", cropId)
        fieldEditorViewModelRes.requestLzField(creatRequestBody(jsonObject.toString()), removeField)
    }

    /**
     * 删除地块
     */
    fun requestRemoveFieldById(fieldId: Long?) = launch {
        if (fieldId == null) return@launch
        val jsonObject = JSONObject()
        jsonObject.put("ids", fieldId)
        fieldEditorViewModelRes.requestRemoveFieldById(
            creatRequestBody(jsonObject.toString()),
            removeField
        )
    }
}